package leetcode.code0653;

import java.util.HashSet;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;
import leetcode.helper.tree.TreeArray;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	public boolean findTarget(TreeNode root, int k) {
		Set<Integer> set = new HashSet<>();
		return findTarget(root, k, set);
	}

	private boolean findTarget(TreeNode root, int k, Set<Integer> set) {
		int cur = root.val;
		if (root.left != null) {
			if (this.findTarget(root.left, k, set)) {
				return true;
			}
		}
		int sub = k - cur;
		if (set.contains(sub)) {
			return true;
		}
		set.add(cur);
		if (root.right != null) {
			if (this.findTarget(root.right, k, set)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(true, this.findTarget(TreeArray.array2tree(HelpDebug.str2array4("[2,0,3,-4,1]")), -1));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub
		HelpDebug.compare(true, this.findTarget(TreeArray.array2tree(HelpDebug.str2array4("[5,3,6,2,4,null,7]")), 9));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub
		HelpDebug.compare(false, this.findTarget(TreeArray.array2tree(HelpDebug.str2array4("[5,3,6,2,4,null,7]")), 28));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
